package com.doophan.duuchin.service.impl;

import com.doophan.duuchin.dto.PageDTO;
import com.doophan.duuchin.dto.QueryDTO;
import com.doophan.duuchin.mapper.SongMapper;
import com.doophan.duuchin.mapper.UserMapper;
import com.doophan.duuchin.model.Song;
import com.doophan.duuchin.service.SongService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SongServiceImpl implements SongService {
    @Resource
    private SongMapper songMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public PageDTO<Song> getList(QueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getLimit());
        List<Song> songList = songMapper.selectList(null);
        songList.forEach( e -> e.setUser(userMapper.selectById(e.getUserId())));
        PageInfo pageInfo = new PageInfo<>(songList);
        PageHelper.clearPage();
        PageDTO pageDTO = PageDTO.builder().total(pageInfo.getTotal()).data(pageInfo.getList()).build();
        return pageDTO;
    }

    @Override
    public Song getInfo(Integer id) {
        return songMapper.selectById(id);
    }
}
